/**
 * CircleMemberDao.java
 * com.jamin.aabill.dao
 *
 * Function： TODO 
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2012-8-30 		wangjieming
 *
 * Copyright (c) 2012, TNT All Rights Reserved.
 */

package com.jamin.aabill.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.jamin.aabill.data.CircleMember;
import com.jamin.aabill.db.BillDataBaseHelper;
import com.jamin.aabill.db.CircleMemberTableMetaData;
import com.jamin.aabill.ui.AABillApplication;

/**
 * ClassName:CircleMemberDao
 *
 * @author   wangjieming
 * @version  1.0
 * @since    v1.0
 * @Date	 2012-8-30 上午10:31:52 
 */
public class CircleMemberDao {


	private static final String TAG = "CircleMemberDao";
	BillDataBaseHelper mDbHelper = null;


	public CircleMemberDao(){
		mDbHelper = AABillApplication.mDB;
	}


	public long replace(CircleMember circleMember){
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		long id = db.replace(CircleMemberTableMetaData.TABLE_NAME, null, getContentValues(circleMember));
		db.close();
		return id;
	}





	public List<CircleMember> getCircleMemberList(long circleId){
		List<CircleMember> list = new ArrayList<CircleMember>();
		SQLiteDatabase db = mDbHelper.getReadableDatabase();
		Cursor cur = db.query(CircleMemberTableMetaData.TABLE_NAME , null , 
				CircleMemberTableMetaData.CIRCLE_ID + "=" + circleId,
				null, null , null, CircleMemberTableMetaData.CREATE_AT + " desc");
		for( cur.moveToFirst();!cur.isAfterLast() ;cur.moveToNext() ){
			list.add(new CircleMember(cur));
		}
		cur.close();
		return list;
	}



	public void Delete(int CircleMemberId){
		SQLiteDatabase db = mDbHelper.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(CircleMemberTableMetaData.ACTIVE, 0);
		db.update(CircleMemberTableMetaData.TABLE_NAME, values, CircleMemberTableMetaData.TABLE_NAME + "=" + CircleMemberId, null);
	}





	private ContentValues getContentValues(CircleMember circleMember){
		ContentValues values = new ContentValues();
		if(circleMember.get_id() > 0){
			values.put(CircleMemberTableMetaData._ID, circleMember.get_id());	
		}
		values.put(CircleMemberTableMetaData.NAME, circleMember.getName());
		values.put(CircleMemberTableMetaData.CIRCLE_ID, circleMember.getCircle_id());
		values.put(CircleMemberTableMetaData.DESCRIPTION, circleMember.getDescription());
		values.put(CircleMemberTableMetaData.CREATE_AT, circleMember.getCreate_at());
		values.put(CircleMemberTableMetaData.MOBILE, circleMember.getMobile());
		return values;

	}







}

